/**
 * Created by Administrator on 2018/7/27/027.
 */
// layui方法

layui.use(['table', 'form', 'layer','element'], function () {

    // 操作对象
    var form = layui.form,
        table = layui.table,
        layer = layui.layer,
        element = layui.element,
        $ = layui.jquery;


    var token = $('#_token').val();
    var tableIns;
    var check_val;  //冻结状态

    // 列表初始化
    getMemberList();

    //刷新
    $('#btn-refresh').on('click', function () {
        tableIns.reload();
    });

    //清空
    $('.clear').on('click', function () {
        location.href='/account'
    });

    //添加
    $('#btn-add').on('click',function(){
        //自定页
        layer.open({
            type: 1,
            title:['添加会员','font-size: 16px;color: #000;font-weight: bolder;'],
            skin: 'layui-layer-rim', //加上边框
            area: ['520px', '446px'], //宽高
            content: $('.add_account'),
            success:function(){

            }

        });
    });



    //导出
    form.on('submit(download)', function (data) {
        layer.confirm('默认导出5000条数据，是否导出', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var search_data = data.field;
            var url ='/downloadMemeberData?page=1&limit=5000'+'&mobile='+search_data.mobile+'&frozen_flag='+search_data.frozen_flag;
            window.open(url,'_blank');
        })

    });
    //工具条操作
    table.on('tool(account_data)', function(obj) {

        //详情
        if(obj.event === 'detail') {
        // 添加TAB选项卡
            location.href = '/acc_detail?uuid=' + obj.data.id + '&flag=' + obj.data.frozen_flag;

        }

        //充值动作
        if(obj.event === 'recharge') {
            var index = layer.load(2, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
                , time: 1000,
                end:function(e){
                    //自定页
                    layer.open({
                        type: 1,
                        title:['充值','font-size: 16px;color: #000;font-weight: bolder;'],
                        skin: 'layui-layer-rim', //加上边框
                        area: ['400px', '360px'], //宽高
                        content: $('.acc_recharge'),
                        success:function(){
                            check_val = obj.data.frozen_flag;
                            getOrderNumber();
                            $('#user_id').val(obj.data.id);
                        }
                    });
                }
            });
        }

        //冻结账号
        if(obj.event === 'del') {
            layer.open({
                type: 1,
                title:['修改会员状态','font-size: 16px;color: #000;font-weight: bolder;'],
                skin: 'layui-layer-rim', //加上边框
                area: ['240px', '200px'], //宽高
                content: $('.acc_update'),
                success:function(){
                    check_val = obj.data.frozen_flag;
                    $('#u_id').val(obj.data.id);
                    $('[name=frozen_flag]').each(function(i,item){
                        if($(item).val()==obj.data.frozen_flag){
                            $(item).prop('checked',true);
                            form.render();
                        }
                    });

                }
            });
        }
    });

    //查询
    form.on('submit(search)', function (data) {
        var search_data = data.field;
        getMemberList(search_data);
    });

    //状态弹窗 提交
    form.on('submit(acc_update)', function (data) {
        var update = data.field;

        if(check_val == update.frozen_flag){
            location.href ='/account';
            return;
        }
        if(update.frozen_flag == 1){
            layer.confirm('确定冻结此账号？冻结以后该账号将无法使用,请谨慎操作', {
                btn: ['仍要冻结','取消'] //按钮
            }, function(){
                forbiddenAccount(update);
            })

        }
        if(update.frozen_flag == 0){
            thawAccount(update);
        }
    });

    //充值提交
    form.on('submit(acc_recharge)', function (data) {
        var recharge = data.field;
        console.log(recharge);
        console.log(check_val);
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    location.href = "/account"
                }
            });
            return;
        }

        accRecharge(recharge)

    });

    //添加提交
    form.on('submit(add_account)', function (data) {
        addAccount(data);
    });

    // 渲染列表
    function getMemberList(data){
        tableIns = table.render({
            elem: '#account_data',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'username', title: '用户名'}
                , {field: 'mobile', title: '手机号'}
                , {field: 'wechat_bind_name', title: '微信绑定'}
                , {field: 'balance', title: '钱包余额'}
                , {field: 'type_name', title: '用户来源'}
                , {field: 'frozen_status', title: '会员状态'}
                , {field: 'created_at', title: '创建时间'}
                , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barOption'} //这里的toolbar值是模板元素的选择器
            ]],
            url: '/getMemberList',
            where: data,    //查询参数
            method: 'get',
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                // console.log(res);

                //得到当前页码
                // console.log(curr);

                //得到数据总量
                // console.log(count);
            }
        });
    }

    //添加会员
    function addAccount(data){
        var data = data.field;
//            var data = JSON.stringify(data.field);
        data._token = token;

        var pass  = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;
        if(data.mobile == ''){
            layer.msg('请填写手机号',{anim:6,icon:5},function(){

            });
            return false;
        }
        if(!pass.test(data.password)) {
            layer.msg('请输入8-20位字母与数字组合密码',{anim:6,icon:5},function(){});
            return false;
        }

        $.ajax({
            url: '/addMember',          //  登录
            type: 'post',
            data: data,
            timeout: 1000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {

                if (data.error_code == 0) {
                    layer.msg('添加成功',{
                        time: 2000, //2s后自动关闭
                        end:function(e){
                            location.href = "/account"
                        }
                    });
                } else {
                    layer.alert('添加失败，请重新添加');
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/account"
                    }});
            }
        })

    }

    //流水号
    function getOrderNumber(){
        $.ajax({
            url: '/getAdminOrder',          //  流水号
            type: 'get',
            data: {},
            timeout: 3000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    $('#serial_number').val(data.data);
                }
            }
        });



    }

    //会员充值
    function accRecharge(obj){

        obj._token = token;

        $.ajax({
            url: '/accRecharge',          //  充值
            type: 'post',
            data: obj,
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('充值成功',{
                        time: 2000,
                        end:function(e){
                            location.href = "/account"
                        }});

                } else {
                    layer.msg('充值失败，请重新操作',{
                        time: 2000,
                        end:function(e){
                            location.href = "/account"
                        }});

                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/account"
                    }});
            }
        });
    }

    //会员状态修改--冻结
    function forbiddenAccount(obj) {
        var for_data = obj;
        $.ajax({
            url: '/forbiddenMem',          //  删除账号
            type: 'post',
            data: {
                u_id: for_data.u_id,
                _token:token,
            },
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('冻结成功',{
                        time: 2000,
                        end:function(e){
                            location.href = "/account"
                        }});

                } else {
                    layer.msg('冻结失败',{
                        time: 2000,
                        end:function(e){
                            location.href = "/account"
                        }});
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/account"
                    }});
            }
        });
    }

    //会员状态修改--解冻
    function thawAccount(obj){
        var for_data = obj;
        $.ajax({
            url: '/thawMember',          //  删除账号
            type: 'post',
            data: {
                u_id: for_data.u_id,
                _token:token,
            },
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('解除冻结成功',{
                        time: 2000,
                        end:function(e){
                            location.href = "/account"
                        }});
                } else {
                    layer.msg('解除冻结失败',{
                        time: 2000,
                        end:function(e){
                            location.href = "/account"
                        }});
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/account"
                    }});
            }
        });
    }

});